package com.wu.client.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.client.constant.RelationShipTypeEnum;
import com.wu.client.constant.ResTypeEnum;
import com.wu.client.service.RelationshipService;
import com.wu.client.service.UserService;
import com.wu.client.utils.MinIoUtil;
import com.wu.client.utils.PageRespUtil;
import com.wu.client.vo.req.user.UserLoginReq;
import com.wu.client.vo.req.user.UserRegisterReq;
import com.wu.client.vo.res.PageResp;
import com.wu.client.vo.res.community.PostResp;
import com.wu.client.vo.res.user.UserLoginResp;
import com.wu.constant.Enum.MinIoNameEnum;
import com.wu.mongo.Post;
import com.wu.mysql.entiy.*;
import com.wu.mysql.mapper.*;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements UserService {
    private final MinIoUtil minIoUtil;
    private final MongoTemplate mongoTemplate;
    private final MvMapper mvMapper;
    private final SongMapper songMapper;
    private final AlbumMapper albumMapper;
    private final PlaylistMapper playlistMapper;
    private final RelationshipService relationshipService;
    private final ArtistMapper artistMapper;
    @Override
    public void register(UserRegisterReq userRegisterReq) {
        UserDO userDO = new UserDO();
        userDO.setUsername(userRegisterReq.getUsername());
        userDO.setPassword(userRegisterReq.getPassword());
        userDO.setEmail(userRegisterReq.getEmail());
        userDO.setRegistrationTime(new Date());
        userDO.setAvatar("/img/avatar/1.jpg");
        save(userDO);
    }

    @Override
    public UserLoginResp login(UserLoginReq userLoginReq) {
        String email = userLoginReq.getEmail();
        String password = userLoginReq.getPassword();
        LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserDO::getEmail, email)
                .eq(UserDO::getPassword, password);
        UserDO userDO = getOne(queryWrapper);
        if (ObjectUtil.isNotNull(userDO)) {
            UserLoginResp userLoginResp = new UserLoginResp();
            userLoginResp.setUserId(userDO.getId());
            userLoginResp.setUsername(userDO.getUsername());
            userLoginResp.setAvatar(userDO.getAvatar());
            userLoginResp.setToken(RandomUtil.randomString(10));
            return userLoginResp;
        }
        return null;
    }


}
